package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.BActivityService;
import cn.huiyunche.base.service.mappers.BActivityMapper;
import cn.huiyunche.base.service.mappers.BActivityRolesMapper;
import cn.huiyunche.base.service.mappers.BActivityTypeMapper;
import cn.huiyunche.base.service.model.*;
import cn.huiyunche.base.service.model.BActivityExample.Criteria;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.assertj.core.util.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BActivityServiceImpl implements BActivityService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BActivityServiceImpl.class);

    @Autowired
    private BActivityMapper bActivityMapper;

    @Autowired
    private BActivityTypeMapper bActivityTypeMapper;

    @Autowired
    private BActivityRolesMapper bActivityRolesMapper;

    @Override
    public BActivity getByTypeAndClass(int userType, int activityType, int activityClass) {
        LOGGER.info("getByTypeAndClass params : {}, {}, {}", activityType, activityClass);
        if (0 == activityType) {
            LOGGER.error("getByTypeAndClass activityType must not be 0");
            throw new IllegalArgumentException("活动类型不能为0");
        }
        if (0 == activityClass) {
            LOGGER.error("getByTypeAndClass activityClass must not be 0");
            throw new IllegalArgumentException("活动分类不能为0");
        }

        BActivityType bActivityType = this.getByCode(activityType);

        List<Long> listParam = new ArrayList<>();
        if (0 != userType) {
            BActivityRolesExample roleExample = new BActivityRolesExample();
            roleExample.createCriteria().andUserTypeEqualTo(userType);
            List<BActivityRoles> roles = bActivityRolesMapper.selectByExample(roleExample);
            for (BActivityRoles bActivityRoles : roles) {
                listParam.add(bActivityRoles.getActivityId());
            }
        }

        BActivityExample example = new BActivityExample();
        Criteria criteria = example.createCriteria().andActivityTypeIdEqualTo(bActivityType.getId()).andActivityClassEqualTo(activityClass).andBeginDateLessThanOrEqualTo(new Date()).andEndDateGreaterThanOrEqualTo(new Date());
        if (!Collections.isNullOrEmpty(listParam)) {
            criteria.andIdIn(listParam);
        }
        List<BActivity> list = bActivityMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public BActivityType getByCode(int code) {
        LOGGER.info("getByCode param : {}", code);
        BActivityTypeExample example = new BActivityTypeExample();
        example.createCriteria().andCodeEqualTo(code);
        List<BActivityType> list = bActivityTypeMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            LOGGER.error("getByCode list is empty");
            throw new BusinessException("获取活动类型失败！");
        }
        return list.get(0);
    }

    @Override
    public BActivity getByRoleAndTypeAndClass(int userType, int activityType, int activityClass) {
        LOGGER.info("getByRoleAndTypeAndClass params : {}, {}, {}", userType, activityType, activityClass);
        if (0 == userType) {
            LOGGER.error("getByRoleAndTypeAndClass userType must not be 0");
            throw new IllegalArgumentException("用户类型不能是0");
        }
        if (0 == activityType) {
            LOGGER.error("getByRoleAndTypeAndClass activityType must not be 0");
            throw new IllegalArgumentException("活动类型不能为0");
        }
        if (0 == activityClass) {
            LOGGER.error("getByRoleAndTypeAndClass activityClass must not be 0");
            throw new IllegalArgumentException("活动分类不能为0");
        }

        BActivity activity = this.getByTypeAndClass(userType, activityType, activityClass);

        if (null != activity) {
            BActivityRoles activityRoles = this.getByActivityAndUserType(activity.getId(), userType);
            if (null != activityRoles) {
                return activity;
            }
        }

        return null;
    }

    @Override
    public BActivityRoles getByActivityAndUserType(long activityId, int userType) {
        LOGGER.info("getByActivityAndUserType params : {}, {}", activityId, userType);
        if (0 == activityId) {
            LOGGER.error("getByActivityAndUserType activityId must not be 0");
            throw new IllegalArgumentException("活动id不能是0");
        }
        if (0 == userType) {
            LOGGER.error("getByActivityAndUserType userType must not be 0");
            throw new IllegalArgumentException("用户类型不能是0");
        }

        BActivityRolesExample example = new BActivityRolesExample();
        example.createCriteria().andActivityIdEqualTo(activityId).andUserTypeEqualTo(userType);
        List<BActivityRoles> list = bActivityRolesMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }

        return null;
    }

}
